import java.util.ArrayList;
import java.util.List;

class Solution1 {
    List<String> list = new ArrayList<>();
    String[] strs = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    StringBuffer str = new StringBuffer();
    public List<String> letterCombinations(String digits) {
        if(digits.length()==0) return list;
        dfs(digits,0);
        return list;
    }
    public void dfs(String digits, int u) {
        if(u == digits.length()) {
            list.add(new String(str));
            System.out.println(str);
            return;
        }
        int len = strs[digits.charAt(u)-'0'].length();
        for(int i = 0; i<len; i++) {
            str.append(strs[digits.charAt(u)-'0'].charAt(i));
            dfs(digits,u+1);
            str.delete(str.length()-1,str.length());
        }
    }
}

class Solution2 {
    int left = 0;
    int right = 0;
    int n = 0;
    List<String> list = new ArrayList<>();
    StringBuffer str = new StringBuffer();
    public List<String> generateParenthesis(int i) {
        n = i;
        dfs();
        return list;
    }
    public void dfs() {
        if(left < right || left > n) return;
        if(left == n && left == right) {
            list.add(new String(str));
            System.out.println(str);
            return;
        }

        str.append("(");
        left++;
        dfs();
        str.deleteCharAt(str.length()-1);
        left--;

        str.append(")");
        right++;
        dfs();
        str.deleteCharAt(str.length()-1);
        right--;
    }
}


class Solution3 {
    boolean[] st;
    List<List<Integer>> lists = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {
        st = new boolean[n+1];
        dfs(0,k, n);
        return lists;
    }
    public void dfs(int u,int k, int n) {
        if(u >= 2 && u < n && list.get(u-2) > list.get(u-1)) return;
        if(u == k) {
            lists.add(new ArrayList<>(list));
            return;
        }
        for(int i = 1; i<=n; i++) {
            if(!st[i]) {
                list.add(i);
                st[i] = true;
                dfs(u+1,k,n);
                list.remove(list.size()-1);
                st[i] = false;
            }
        }
    }
}


class Solution4 {
    List<List<Integer>> lists = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {
        dfs(1,k, n);
        return lists;
    }
    public void dfs(int u,int k, int n) {
        if(list.size() == k) {
            lists.add(new ArrayList<>(list));
            return;
        }
        for(int i = u; i<=n; i++) {
            list.add(i);
            dfs(i+1,k,n);
            list.remove(list.size()-1);
        }
    }
}

class Solution {
    int sum = 0;
    int target = 0;
    int count = 0;
    public int findTargetSumWays(int[] nums, int _target) {
        target = _target;
        dfs(0,nums);
        return count;
    }
    public void dfs(int u, int[] nums) {
        if(u == nums.length) {
            if(sum == target) {
                count++;
            }
            return;
        }

        //+
        sum += nums[u];
        dfs(u+1,nums);
        sum -= nums[u];

        //-
        sum -= nums[u];
        dfs(u+1,nums);
        sum += nums[u];
    }
}



public class Test {
    public static void main(String[] args) {
        List<List<Integer>> lists = new Solution3().combine(4,2);
    }
    public static void main2(String[] args) {
        List<String > ret = new Solution2().generateParenthesis(2);
    }
    public static void main1(String[] args) {
        List<String > ret = new Solution1().letterCombinations("23456789");
    }
}
